package com.liumuxian.video.web.controller;

import com.liumuxian.video.client.OrdersClient;
import com.liumuxian.video.pojo.Video;
import com.liumuxian.video.service.VideoService;
import entity.*;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.*;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.*;

@RestController
@CrossOrigin
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Value("${file.uploadFolder}")
    private String uploadFolder;
    @Autowired
    private OrdersClient ordersClient;

    /**
     * 添加
     * @param video
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/videoAdd")
    public Result add(Video video, @RequestParam(value = "file", required = true) MultipartFile file) throws IOException{
        byte[] bytes = file.getBytes();
        String year = DateTime.now().toString("yyyy")+"\\"+DateTime.now().toString("MM")+"\\"+DateTime.now().toString("dd");
        //Office OFFICE功能模块、Accountant 会计考证功能模块、Teacher 教师资格证功能模块、SoftTest 软考功能模块
        String sub[]={"Office","Accountant","Teacher","SoftTest"};
        String uploadFolder=this.uploadFolder+sub[0]+"\\"+year;
        File fileToSave = new File(uploadFolder+"\\",file.getOriginalFilename());
        if(!fileToSave.getParentFile().exists()){//判断服务器当前路径文件夹是否存在
            fileToSave.getParentFile().mkdirs();//不存在则创建文件夹
        }
        FileCopyUtils.copy(bytes, fileToSave);
        video.setUrl(file.getOriginalFilename());
        videoService.save(video);
        return new Result(true, StatusCode.OK,  "保存成功",video);
    }

    /**
     * 根据模板id查询视频
     * @param tid
     * @return
     */
    @RequestMapping("findByTemAndVideo/{tid}")
    public Result listvido(@PathVariable String tid){
        List<Video> video = videoService.findByTemAndVideo(tid);
        HashSet<String> id = new HashSet<>();
        video.forEach(v ->id.add(v.getId()));
        Result result =  ordersClient.shoppurc(id,"1206421096698613760");
        if(result!=null && result.getCode() == 0){
            Map<String,String> map = (Map<String, String>) result.getData();
            video.forEach(v -> {
                Boolean fig= Boolean.parseBoolean(map.get(v.getId()));
                if(fig){
                    v.setType(2);
                }
            });
        }
        return new Result(true, StatusCode.OK,  "查询成功",video);
    }

    /**
     * 删除
     * @param vid
     * @return
     */
    @RequestMapping("videoDelete/{vid}")
    public Result delete(@PathVariable String vid){
        Video video=videoService.getOne(vid);
        video.setState(0);
        videoService.update(video);
        return new Result(true, StatusCode.OK,  "删除成功",video);
    }

    /**
     * 修改
     * @param video
     * @return
     */
    @RequestMapping("videoUpdate")
    public Result update(Video video){
        video.setId("1205410591221616640");
        video.setType(0);
        video.setState(0);
        video.setCount(1156.23);
        video.setPrice(120.00);
        video.setCreatdata(new Date());
        video.setUpdatedate(new Date());
        videoService.update(video);
        return new Result(true, StatusCode.OK,  "修改成功",video);
    }

    /**
     * 根据id查询
     * @param vid
     * @return
     */
    @RequestMapping("videoFindById/{vid}")
    public Result findById(@PathVariable String vid){
        Video v=videoService.getOne(vid);
        return new Result(true, StatusCode.OK,  "查询成功",v);
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("videoFindAll")
    public Result findAll(){
        List<Video> list=videoService.findAllByState(1);
        for (Video v:list) {
            System.out.println(v.toString());
        }
        return new Result(true, StatusCode.OK,  "查询成功",list);
    }
}
